<script setup name="Lead">
import {leadGetCommentList, leadGetList} from "@/api/hrp/lead";
import LeadCurd from "@/views/hrp/lead/curd";
import {getCurrentInstance, onMounted, reactive} from "vue";
import {useRoute} from "vue-router";

const {proxy} = getCurrentInstance();

const route = useRoute();
const {hrp_lead_lost_reason, hrp_lead_source,hrp_lead_stage} = proxy.useDict('hrp_lead_lost_reason', 'hrp_lead_source','hrp_lead_stage');

onMounted(() => {
  pd.queryParams = {...pd.queryParams, ...route.query};
  getList();
});

const pd = reactive({
  leadList: [],
  loading: true,
  showSearch: true,
  total: 0,
  daterangeUpdate_time: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    enableFlag: "0"
  },
});


/*** 跟踪记录 */
const comment = reactive({
  open: false,
  title: "跟踪记录详情",
  list: []
});

function getCommentList(row) {
  comment.open = true;
  leadGetCommentList({id: row.id}).then(res => {
    comment.list = res.data;
  });
};

/** 查询合作伙伴列表 */
function getList() {
  pd.loading = true;
  if (null != pd.daterangeUpdate_time && '' != pd.daterangeUpdate_time) {
    pd.queryParams.beginTime = pd.daterangeUpdate_time[0];
    pd.queryParams.endTime = pd.daterangeUpdate_time[1];
  }
  leadGetList(pd.queryParams).then(res => {
    pd.leadList = res.data;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.beginTime = null;
  pd.queryParams.endTime = null;
  pd.queryParams.name = null;
  pd.daterangeUpdate_time = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  let lead = {};
  proxy.$refs["leadCurdRef"].add(lead);
}

/** 修改按钮操作 */
function handleEdit(lead) {
  proxy.$refs["leadCurdRef"].edit(lead);
}

/** 修改按钮操作 */
function handleDel(id) {
  proxy.$refs["leadCurdRef"].del(id);
}

/** 对话框完成后刷新界面 */
function leadCurdOk(operation) {
  getList();
}

</script>

<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="" prop="name">
        <el-input :style="{width: '120px'}" v-model="pd.queryParams.name" placeholder="请输入名称" clearable
                  @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="" prop="mobile">
        <el-input :style="{width: '120px'}" v-model="pd.queryParams.mobile" placeholder="请输入手机号" clearable
                  @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item label="" prop="stage">
        <el-select :style="{width: '150px'}" v-model="pd.queryParams.stage" placeholder="请选择销售阶段" clearable>
          <el-option v-for="item in hrp_lead_stage" :key="item.code" :label="item.name" :value="item.code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="source">
        <el-select :style="{width: '150px'}" v-model="pd.queryParams.source" placeholder="请选择客户来源" clearable>
          <el-option v-for="item in hrp_lead_source" :key="item.code" :label="item.name" :value="item.code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker v-model="pd.daterangeUpdate_time" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['hrp:lead:add']">新 增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.leadList">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" prop="name">
        <template #default="scope">
          <el-link type="primary" @click="handleEdit(scope.row)" v-hasPermi="['hrp:lead:edit']">
            {{ scope.row.name }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="userName"/>
      <el-table-column label="客户来源" align="center" prop="source">
        <template #default="scope">
          <dict-tag :options="hrp_lead_source" :value="scope.row.source"/>
        </template>
      </el-table-column>
      <el-table-column label="手机" align="center" prop="mobile" width="120"/>
      <el-table-column label="跟踪备注" align="center" prop="remark">
        <template #default="scope">
          <el-link type="primary" @click="getCommentList(scope.row)" v-hasPermi="['hrp:lead:comment']">
            {{ scope.row.remark }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="销售阶段" align="center" prop="stage">
        <template #default="scope">
          <dict-tag :options="hrp_lead_stage" :value="scope.row.stage"/>
        </template>
      </el-table-column>
      <el-table-column label="跟单时间" align="center" prop="nextTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.nextTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="220">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="More" @click="getCommentList(scope.row)" v-hasPermi="['hrp:lead:comment']">
            详情
          </el-button>
          <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)" v-hasPermi="['hrp:lead:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDel(scope.row.id)"
                     v-hasPermi="['hrp:lead:del']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 跟单详情 -->
    <el-dialog :title="comment.title" v-model="comment.open" width="500px" append-to-body>
      <el-timeline :reverse="true">
        <el-timeline-item v-for="(item, index) in comment.list" :key="index" :timestamp="item.createTime">
          <strong>
            <dict-tag :options="hrp_lead_stage" :value="item.stage"/>
            :</strong>
          <strong>{{ item.createBy }}:</strong>{{ item.remark }}
        </el-timeline-item>
      </el-timeline>
      <div slot="footer" class="dialog-footer">
        <el-button @click="comment.open = false">关闭</el-button>
      </div>
    </el-dialog>

    <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
    <LeadCurd ref="leadCurdRef" @ok="leadCurdOk"/>
  </div>
</template>

<style lang='scss' scoped>

</style>